package com.stlm2.dems.service;

import com.stlm2.dems.dao.IntegralGoodsDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.*;
import com.stlm2.dems.user.dao.AccountDao;
import com.stlm2.dems.user.entity.Account;
import com.stlm2.dems.util.CoreLogic;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.dao.IntegralOrderDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("integralOrderService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class IntegralOrderService extends BaseServiceImpl<IntegralOrder> {

	@Resource
	private IntegralOrderDao integralOrderDao;
	@Resource
	private IntegralGoodsDao integralGoodsDao;
	@Resource
	private AccountDao accountDao;
	@Resource
	private CoreLogic coreLogic;
	@Resource
	private UserDao userDao;
	@Resource
	private PeriodsService periodsService;

	@Override
	protected BaseDao <IntegralOrder> getDao() {
		return integralOrderDao;
	}

    public IntegralOrder pay(Account account, IntegralOrder integralOrder) {
		if(integralOrder.getOrderState().equals(OrderState.Payment))
			return integralOrder;

		BigDecimal payIntegral=integralOrder.getPayIntegral();

		account.setRemainJiFen(account.getRemainJiFen().subtract(payIntegral));
		account.setReducedJiFen(account.getReducedJiFen().add(payIntegral));


		integralOrder.setOrderState(OrderState.Payment);
		if(integralOrder.getGoodsType().equals(GoodsType.IntegralPrize))
			integralOrder.setPayChannel(PayChannel.IntegralPrize);
		else
			integralOrder.setPayChannel(PayChannel.IntegralConversion);
		integralOrder.setPayTime(new Date());



		accountDao.modify(account);
		integralOrderDao.modify(integralOrder);
		coreLogic.addFundsFlow(integralOrder.getUserId(),payIntegral, FlowSource.ConvertIntegral, FlowType.Reduce, integralOrder.getId(), integralOrder.getCode());
		return integralOrder;
    }

	public IntegralOrder createOrder(IntegralGoods integralGoods, Address address, Integer num, User user,Integer periodsId) {

		BigDecimal unitIntegral=integralGoods.getUnitIntegral();
		BigDecimal payIntegral=unitIntegral.multiply(new BigDecimal(num));

		IntegralOrder integralOrder=new IntegralOrder();
		integralOrder.setCode("F"+user.getId()+new Date().getTime());
		integralOrder.setCreateTime(new Date());
		integralOrder.setGoodsId(integralGoods.getId());
		integralOrder.setNum(num);
		integralOrder.setOrderState(OrderState.Create);
		integralOrder.setPayIntegral(payIntegral);
		integralOrder.setUnitIntegral(unitIntegral);
		integralOrder.setUserId(user.getId());
		integralOrder.setName(address.getName());
		integralOrder.setPhone(address.getPhone());
		integralOrder.setCity(address.getCity());
		integralOrder.setAddressDetail(address.getDetail());
		integralOrder.setGoodsType(integralGoods.getGoods_type());
		if(integralGoods.getGoods_type().equals(GoodsType.IntegralPrize))
			integralOrder.setPeriodsId(periodsId);

		integralGoods.setInventory(integralGoods.getInventory()-num);

		integralGoodsDao.modify(integralGoods);
		integralOrderDao.add(integralOrder);
		return integralOrder;
	}

	public IntegralOrder getByCode(String code) {
		return integralOrderDao.getByCode(code);
	}

	public List<ModelMap> convertModel(List<IntegralOrder> integralOrderList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (integralOrderList != null && integralOrderList.size() > 0) {
			for (IntegralOrder integralOrder : integralOrderList) {
				ModelMap map = buildModel(integralOrder);
				list.add(map);
			}
		}
		return list;
	}

	public ModelMap buildModel(IntegralOrder integralOrder) {
		ModelMap model = new ModelMap();
		User user=userDao.getById(integralOrder.getUserId());
		IntegralGoods integralGoods=integralGoodsDao.getById(integralOrder.getGoodsId());
		Periods periods=periodsService.findEntityById(integralOrder.getPeriodsId());
		model.put("id",integralOrder.getId());
		model.put("name",user.getRealName()!=null?user.getRealName():user.getName());
		model.put("code",integralOrder.getCode());
		model.put("state",integralOrder.getOrderState().toString());
		model.put("stateValue",integralOrder.getOrderState().getIntValue());
		model.put("payIntegral",integralOrder.getPayIntegral());
		model.put("payTime",integralOrder.getPayTime()==null?"": Convert.toFullYMDHMS(integralOrder.getPayTime()));
		model.put("payChannel",integralOrder.getPayChannel()!=null?integralOrder.getPayChannel().toString():"");
		model.put("createTime",Convert.toFullYMDHMS(integralOrder.getCreateTime()));
		model.put("city",integralOrder.getCity()!=null?integralOrder.getCity():"");
		model.put("addressDetail",integralOrder.getAddressDetail()!=null?integralOrder.getAddressDetail():"");
		model.put("name",integralOrder.getName()!=null?integralOrder.getName():"");
		model.put("phone",integralOrder.getPhone()!=null?integralOrder.getPhone():"");
		model.put("cancelTime",integralOrder.getCancelTime()!=null?Convert.toFullYMDHMS(integralOrder.getCancelTime()):"");
		model.put("goodsName",integralGoods.getName());
		model.put("num",integralOrder.getNum());
		model.put("goodsImg",integralGoods.getMainImg());
		model.put("expressType",integralOrder.getExpressType()!=null?integralOrder.getExpressType().toString():"");
		model.put("wayBillNum",integralOrder.getWayBillNum()!=null?integralOrder.getWayBillNum():"");
		model.put("receiptEndTime",integralOrder.getReceiptEndTime()!=null?Convert.toFullYMDHMS(integralOrder.getReceiptEndTime()):"");
		model.put("periodsCode",periods!=null?periods.getCode():"");
		model.put("openTime",integralOrder.getOpenTime()!=null?Convert.toFullYMDHMS(integralOrder.getOpenTime()):"");
		model.put("goodsType",integralOrder.getGoodsType().toString());
		model.put("goodsTypeValue",integralOrder.getGoodsType().getIntValue());
		model.put("prizeState", integralOrder.getOpenTime()!=null ?integralOrder.getPrizeState()?"已中奖":"未中奖":"");
		model.put("prizeStateValue", integralOrder.getPrizeState()!=null ?integralOrder.getPrizeState().booleanValue():"");

		return  model;
	}
}